package com.car.account.util.aliyunUtil;

import java.io.IOException;

import com.car.account.exception.AccountException;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 云亿互通实名认证
 */
public class AliyunIDcardValidator {

    public static String postData(String name, String idNo) throws IOException {
        String result = "";
        //appCode
        String appCode = "0ec20ded220d42f79e25c3a3e0e0a363";
        //url
        String url = "https://idenauthen.market.alicloudapi.com/idenAuthentication";
        RequestBody formBody = new FormBody.Builder().add("name", name).add("idNo", idNo).build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(formBody).build();
        Call call = new OkHttpClient().newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            throw new AccountException(1111,"网络异常");
        }

        assert response != null;
        if (!response.isSuccessful()) {      // 当返回结果发生错误时
            // 状态码为403时一般是套餐包用尽，需续购；注意：续购不会改变秘钥（appCode），仅增加次数
            // 续购链接：https://market.aliyun.com/products/57000002/cmapi025518.html
            throw new AccountException(response.code(),response.message());
        }
        result = response.body().string();    //此处不可以使用toString()方法，该方法已过期

        return result;
    }
}
